% The outer Aalborg theme is derived from the outer sidebar theme which has the following
% copyright notice:
% Copyright 2007 by Till Tantau
% This file may be distributed and/or modified under the GNU Public License.
%
% See the file doc/licenses/LICENSE for more details.
% <end of copyright notice>
%
% All the changes are (c) 2012 by Jesper Kjær Nielsen <jkn@es.aau.dk> and are
% provided under the same license. (GPLv3)
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You can find the GNU General Public License at <http://www.gnu.org/licenses/>.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerouterthemeUCF}[2012/01/17 v0.1.1 The Aalborg Beamer Theme]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load required packages
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\RequirePackage{tikz}
\RequirePackage{calc}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Theme options and definitions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%theme options
\newlength{\beamer@width}
\setlength{\beamer@width}{2cm}
\DeclareOptionBeamer{width}{\beamer@width=#1}
\DeclareOptionBeamer{left}{\def\beamer@sidebarside{left}}
\DeclareOptionBeamer{right}{\def\beamer@sidebarside{right}}

\DeclareOptionBeamer{hideothersubsections}[]{\beamer@nav@subsectionstyle{show/show/hide}}
\DeclareOptionBeamer{hideallsubsections}[]{\beamer@nav@subsectionstyle{hide}}
\DeclareOptionBeamer{hidetitle}{\def\beamer@hidetitle{true}}
\DeclareOptionBeamer{hideauthor}{\def\beamer@hideauthor{true}}
\DeclareOptionBeamer{hideinstitute}{\def\beamer@hideinst{true}}

\DeclareOptionBeamer{shownavsym}{\def\beamer@shownavsym{true}}

\ExecuteOptionsBeamer{right}
\ProcessOptionsBeamer


%the height of the header is 2.5 times the lineheight of the frame title
\newlength{\beamer@height}
\usebeamerfont{frametitle} %use the frame title font
\setlength{\beamer@height}{2.5\baselineskip} 
\reset@font %reset fonts
\def\beamer@lefttext{left} %used for comparison below
%width of vertical bar separating the main text from the sidebar
\newlength{\beamer@barwidth}
\setlength{\beamer@barwidth}{\beamer@height/10} %the bar width depends on the header height (by a factor of 10)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%beamer specific options
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mode<presentation>%refers to the first four modes (beamer,handout,second and trans). That is, to all modes except the article mode
{
  \ifbeamercolorempty[fg]{Aalborg}{%
    %define the Aalborg color if it is not defined (which may be due to that the Aalborg color theme is not loaded)
    \setbeamercolor{Aalborg}{use={structure,palette sidebar primary},fg=palette sidebar primary.fg,bg=structure.fg}
  }{%
    %aau header is defined - nothing to do
  }
  \setbeamersize{sidebar width \beamer@sidebarside=\beamer@width}
  \setbeamersize{text margin left=0.5cm,text margin right=0.5cm}
  
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %templates
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %frame title
  \defbeamertemplate*{frametitle}{Aalborg theme}{%
    \begin{minipage}[c][\beamer@height][c]{\textwidth}
      {\usebeamercolor[fg]{frametitle}\usebeamerfont{frametitle}\insertframetitle\par}%
      {\usebeamercolor[fg]{framesubtitle}\usebeamerfont{framesubtitle}\insertframesubtitle\par}%
    \end{minipage}
  }
  
  %sidebar
  \defbeamertemplate*{sidebar \beamer@sidebarside}{Aalborg theme}{%
    \Aalborgbackground%
    \vspace{\beamer@height}%
    \vspace{2\baselineskip}\par%
    \ifx\beamer@hidetitle\undefined% insert short title
      \vspace{-\baselineskip}
      \hspace{0.05\beamer@width}%
      {% 
        \usebeamercolor[fg]{title in sidebar}
        \usebeamerfont{title in sidebar}%
        \insertshorttitle[width={0.9\beamer@width},center,respectlinebreaks]
      }%
      \hspace{0.05\beamer@width}
      \vspace{2\baselineskip}\par%
    \fi
    \ifx\beamer@hideauthor\undefined% insert short author
      \vspace{-\baselineskip}
      \hspace{0.05\beamer@width}%
      {%
        \usebeamercolor[fg]{author in sidebar}
        \usebeamerfont{author in sidebar}%
        \insertshortauthor[width={0.9\beamer@width},center,respectlinebreaks]
      }
      \hspace{0.05\beamer@width}
      \vspace{\baselineskip}\par%
    \fi
    \insertverticalnavigation{\beamer@width}%
    \strut\vfill%
    \ifx\beamer@hideinst\undefined% insert short institute
      \vbox{\hspace{0.05\beamer@width}%
      {%
        \usebeamercolor[fg]{title in sidebar}
        \usebeamerfont{subtitle in sidebar}%
        \insertshortinstitute[width={0.9\beamer@width},center,respectlinebreaks]
      }
      \hspace{0.05\beamer@width}%
      \vspace*{\baselineskip}
      }%
    \fi
    \ifx\beamer@shownavsym\undefined% insert navigation symbols
      %do nothing
    \else
      %insert the navigation symbols if the sidebar is on the right
      \ifx\beamer@sidebarside\beamer@lefttext %sidebar on the left
        %do nothing
      \else
        \vspace*{-2\baselineskip}
        \llap{\usebeamertemplate***{navigation symbols}\hskip0.4cm}
        \vskip2pt
      \fi
    \fi
  }
  
% When the sidebar is on the left, the default theme places a logo in the bottom right of the slide (this is NOT the case, when the sidebar is on the right). We remove this logo.
  \ifx\beamer@sidebarside\beamer@lefttext %sidebar on the left
    \defbeamertemplate*{sidebar right}{Aalborg theme}{%
      % remove the logo and insert the navigation symbols if option is set
      \ifx\beamer@shownavsym\undefined% insert navigation symbols
        %do nothing
      \else %insert navigation symbols
        \vfill%
        \llap{\usebeamertemplate***{navigation symbols}\hskip0.4cm}
        \vskip2pt
      \fi
    }
  \fi

  % current section in the sidebar
  \defbeamertemplate*{section in sidebar}{Aalborg theme}{%
    \vbox{%
      \vskip1ex% add some extra space when inserting a new section
      \sidebarnavitem{4pt}{section in sidebar}{%
        \sidebarcurframe{-4pt}{\insertframenumber}%
        \insertsectionhead
      }
    }
  }

  % all section in the sidebar but the current
  \defbeamertemplate*{section in sidebar shaded}{Aalborg theme}{%
    \vbox{%
      \vskip1ex% add some extra space when inserting a new section
      \sidebarnavitem{4pt}{section in sidebar shaded}{\insertsectionhead}
    }
  }

  % current subsection in the sidebar
  \defbeamertemplate*{subsection in sidebar}{Aalborg theme}{%
    \sidebarnavitem{6pt}{subsection in sidebar}{
      \sidebarcurframe{-6pt}{\insertframenumber}%
      \insertsubsectionhead
    }
  }

  % all subsection in the sidebar but the current
  \defbeamertemplate*{subsection in sidebar shaded}{Aalborg theme}{%
    \sidebarnavitem{6pt}{subsection in sidebar shaded}{\insertsubsectionhead}
  }

  % current subsubsection in the sidebar
  \defbeamertemplate*{subsubsection in sidebar}{Aalborg theme}{%
    \sidebarnavitem{8pt}{subsubsection in sidebar}{
      \sidebarcurframe{-8pt}{\insertframenumber}%
      \insertsubsubsectionhead
    }  
  }

  % all subsubsection in the sidebar but the current
  \defbeamertemplate*{subsubsection in sidebar shaded}{Aalborg theme}{%
    \sidebarnavitem{8pt}{subsubsection in sidebar shaded}{\insertsubsubsectionhead} 
  }
  
  %remove the navigation symbols from the title page if the option is not set
  \ifx\beamer@shownavsym\undefined% insert navigation symbols
    \setbeamertemplate{navigation symbols}{%
      %disable navigation symbols
    }
  \fi
  

}%end of beamer specific options

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Macros used in the theme 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% the background in the header and the sidebar
\newcommand{\Aalborgbackground}[0]{%
  \begin{tikzpicture}[remember picture,overlay]
    \ifx\beamer@sidebarside\beamer@lefttext% sidebar on the left
      \coordinate (UC) at (\paperwidth,0); %upper right corner of the slide
      \coordinate (LC) at (0,-\paperheight); %lower left corner of the slide
      \coordinate (BS) at (\paperwidth,-\beamer@height); %start coordinate of the bar
      \coordinate (BE) at (\beamer@width+\beamer@barwidth/2,-\paperheight); %end coordinate of the bar
    \else% sidebar on the right
      \coordinate (UC) at (-\paperwidth+\beamer@width,0); %upper left corner of the slide
      \coordinate (LC) at (\beamer@width,-\paperheight); %lower right corner of the slide
      \coordinate (BS) at (-\paperwidth+\beamer@width,-\beamer@height); %start coordinate of the bar
      \coordinate (BE) at (-\beamer@barwidth/2,-\paperheight); %end coordinate of the bar
    \fi
    \coordinate (IS) at (BS -| BE); %intersection
    \coordinate (logopos) at (\beamer@width/2,-\beamer@height/2);%coordinate of the logo
    %draw the background of the header and the sidebar
    {\usebeamercolor{palette sidebar primary}%
    \fill[fill=palette sidebar primary.bg] (BS) {[rounded corners=8pt] -- (IS)} -- (BE) -- (LC) |- (UC) -- cycle;}
    %draw the thick line
    {\usebeamercolor{Aalborg}%
    \draw[color=fg,line width=\beamer@barwidth,rounded corners=8pt] (BS) -- (IS) -- (BE);
    }
    %insert the total number of frames
    {%
      \usebeamercolor{palette sidebar primary}%
      \usebeamercolor{Aalborg}%
      \usebeamerfont{subsection in sidebar}%
      \node[fill=palette sidebar primary.bg,draw=Aalborg.fg,circle,very thick,minimum width=4mm] at ([yshift=2mm] BE) {{\usebeamercolor[fg]{subsection in sidebar}\inserttotalframenumber}};
    }
    %insert the logo
    \node at (logopos) {\insertlogo};
  \end{tikzpicture}%
}

% sidebar navigation item  
\newcommand{\sidebarnavitem}[3]{%
  \begin{beamercolorbox}[wd=\beamer@width,leftskip=#1,rightskip=1ex plus1fil,vmode]{#2}
    \vbox{}% insert a blank line
    #3\par%
    \vbox{}% insert a blank line
    \vskip-1.5ex%
  \end{beamercolorbox}
}

% current frame number
\newcommand{\sidebarcurframe}[2]{%
  \begin{tikzpicture}[remember picture,overlay]
  \coordinate (CS) at (0,0.8ex); % coordinate of the current section
  \coordinate (CF) at (IS |- CS); % coordinate of the current frame number
  {%
    \usebeamercolor{palette sidebar primary}%
    \usebeamercolor{Aalborg}%
    \draw[color=bg,line width=\beamer@barwidth,rounded corners=8pt] (BS) -| (CF);
    \node[fill=palette sidebar primary.bg,draw=Aalborg.bg,circle,minimum width=3.5mm,thick] at (CF) {{\fontsize{4}{5}\selectfont{\usebeamercolor[fg]{subsection in sidebar}#2}}};
  }
  \end{tikzpicture}%
}

\mode<all>
